package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.pojo.AddRouteAxesReq;
import com.ruoyi.common.pojo.AddSysMonitorReq;
import com.ruoyi.common.pojo.UpdateMonitorDeviceReq;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.SysMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author syj
 * @description 针对表【sys_monitor(固定监测点信息表)】的数据库操作Service实现
 * @createDate 2022-07-26 09:12:52
 */
@Service
public class SysMonitorServiceImpl extends ServiceImpl<SysMonitorMapper, SysMonitor> implements SysMonitorService {

    @Autowired
    SysMonitorMapper sysMonitorMapper;

    @Autowired
    SysMonitorDeviceMapper sysMonitorDeviceMapper;

    @Autowired
    SysDeviceTypeMapper sysDeviceTypeMapper;

    @Autowired
    SysDeviceInfoMapper sysDeviceInfoMapper;

    @Autowired
    SysCruiseDeviceMapper sysCruiseDeviceMapper;

    @Autowired
    SysRouteAxesMapper sysRouteAxesMapper;

    @Autowired
    SysMonitorTypeMapper sysMonitorTypeMapper;

    @Override
    public AjaxResult addMonitor(AddSysMonitorReq params) {

        Date nowDate = DateUtils.getNowDate();
        SysMonitor sysMonitor = new SysMonitor();
        sysMonitor.setMonitorName(params.getMonitorName());
        sysMonitor.setCreateTime(nowDate);
        sysMonitor.setState(1);
        sysMonitor.setType(params.getType());
        sysMonitor.setUserId(params.getUserId());
        sysMonitor.setNickName(params.getNickName());
        sysMonitor.setCentreLatitude(params.getCentreLatitude());
        sysMonitor.setCentreLongitude(params.getCentreLongitude());
        sysMonitorMapper.insert(sysMonitor);

        List<AddRouteAxesReq> axes = params.getAxes();
        if (Objects.nonNull(axes)) {
            axes.forEach(
                    v -> {
                        SysRouteAxes sysRouteAxes = SysRouteAxes.builder()
                                .route(sysMonitor.getId())
                                .latitude(v.getLatitude())
                                .longitude(v.getLongitude())
                                .createTime(nowDate)
                                .sort(v.getSort())
                                .state(1)
                                .build();
                        sysRouteAxesMapper.insert(sysRouteAxes);
                    }
            );
        }

        return AjaxResult.success(sysMonitor.getId());
    }

    @Override
    public AjaxResult queryMonitor(SysMonitor params) {
        List<SysMonitor> sysMonitors = sysMonitorMapper.selectList(Wrappers.<SysMonitor>lambdaQuery()
                .eq(SysMonitor::getState, 1)
                .eq(Objects.nonNull(params.getType()), SysMonitor::getType, params.getType())
        );

        sysMonitors.forEach(v -> {
            List<SysRouteAxes> sysRouteAxesList = sysRouteAxesMapper.selectList(Wrappers.<SysRouteAxes>lambdaQuery().eq(SysRouteAxes::getRoute, v.getId()));
            v.setRouteAxesList(sysRouteAxesList);
            SysMonitorType sysMonitorType = sysMonitorTypeMapper.selectOne(Wrappers.<SysMonitorType>lambdaQuery().eq(SysMonitorType::getId, v.getType()));
            if (sysMonitorType != null) {
                v.setTypeName(sysMonitorType.getTypeName());
            }
        });
        return AjaxResult.success(sysMonitors);
    }

    @Override
    public AjaxResult deleteMonitor(SysMonitor params) {
        SysMonitor sysMonitor = new SysMonitor();
        sysMonitor.setId(params.getId());
        sysMonitor.setState(0);
        sysMonitorMapper.updateById(sysMonitor);

        SysMonitorDevice sysMonitorDevice = new SysMonitorDevice();
        sysMonitorDevice.setState(0);
        sysMonitorDeviceMapper.update(sysMonitorDevice, Wrappers.<SysMonitorDevice>lambdaQuery().eq(SysMonitorDevice::getMonitorId, params.getId()));
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateMonitor(SysMonitor params) {
        sysMonitorMapper.updateById(params);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult queryMonitorDevice(SysMonitor params) {
        List<SysMonitorDevice> sysMonitorDevices = sysMonitorDeviceMapper.selectList(Wrappers.<SysMonitorDevice>lambdaQuery().eq(SysMonitorDevice::getMonitorId, params.getId()).eq(SysMonitorDevice::getState, 1));
        ArrayList<SysDeviceInfo> list = new ArrayList<>();
        sysMonitorDevices.forEach(v -> {
            Integer deviceId = v.getDeviceId();
            SysDeviceInfo sysDeviceInfo = sysDeviceInfoMapper.selectOne(Wrappers.<SysDeviceInfo>lambdaQuery().eq(SysDeviceInfo::getId, deviceId).eq(SysDeviceInfo::getState, 1));
            if (Objects.nonNull(sysDeviceInfo)) {
                SysDeviceType sysDeviceType = sysDeviceTypeMapper.selectOne(Wrappers.<SysDeviceType>lambdaQuery().eq(SysDeviceType::getId, sysDeviceInfo.getDeviceType()).eq(SysDeviceType::getState, 1));
                sysDeviceInfo.setDeviceTypeName(sysDeviceType.getName());
                list.add(sysDeviceInfo);
            }
        });
        return AjaxResult.success(list);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}, propagation = Propagation.REQUIRES_NEW)
    public AjaxResult addMonitorDevice(UpdateMonitorDeviceReq params) {

        List<Integer> deviceIds = params.getDeviceIds();
        SysMonitorDevice sysMonitorDevice = null;

        Date nowDate = DateUtils.getNowDate();
        for (Integer deviceId : deviceIds) {
            sysMonitorDevice = new SysMonitorDevice();
            sysMonitorDevice.setDeviceId(deviceId);
            sysMonitorDevice.setMonitorId(params.getMonitorId());
            sysMonitorDevice.setCreateTime(nowDate);
            sysMonitorDevice.setState(1);
            sysMonitorDeviceMapper.insert(sysMonitorDevice);
        }

        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteMonitorDevice(UpdateMonitorDeviceReq params) {
        List<Integer> deviceIds = params.getDeviceIds();
        SysMonitorDevice sysMonitorDevice = new SysMonitorDevice();
        sysMonitorDevice.setState(1);
        sysMonitorDeviceMapper.update(sysMonitorDevice, Wrappers.<SysMonitorDevice>lambdaQuery().in(SysMonitorDevice::getDeviceId, deviceIds).eq(SysMonitorDevice::getMonitorId, params.getMonitorId()));

        return AjaxResult.success();
    }

    @Override
    public AjaxResult queryDevice(UpdateMonitorDeviceReq params) {
        ArrayList<SysDeviceInfo> sysDeviceInfosNew = new ArrayList<>();
        List<SysDeviceInfo> sysDeviceInfos = sysDeviceInfoMapper.selectList(Wrappers.<SysDeviceInfo>lambdaQuery().eq(SysDeviceInfo::getState, 1));
        if (!sysDeviceInfos.isEmpty()) {
            sysDeviceInfos.forEach(v -> {
                Integer id = v.getId();
                SysCruiseDevice sysCruiseDevice = sysCruiseDeviceMapper.selectOne(Wrappers.<SysCruiseDevice>lambdaQuery().eq(SysCruiseDevice::getDeviceId, id).eq(SysCruiseDevice::getState, 1));
                SysMonitorDevice sysMonitorDevice = sysMonitorDeviceMapper.selectOne(Wrappers.<SysMonitorDevice>lambdaQuery().eq(SysMonitorDevice::getDeviceId, id).eq(SysMonitorDevice::getState, 1));
                if (Objects.isNull(sysCruiseDevice) && Objects.isNull(sysMonitorDevice)) {
                    SysDeviceType sysDeviceType = sysDeviceTypeMapper.selectOne(Wrappers.<SysDeviceType>lambdaQuery().eq(SysDeviceType::getDeviceType, v.getDeviceType()));
                    v.setDeviceTypeName(sysDeviceType.getName());
                    sysDeviceInfosNew.add(v);
                }
            });
        }
        return AjaxResult.success(sysDeviceInfosNew);
    }

    @Override
    public AjaxResult queryMonitorType(SysMonitorType params) {
        List<SysMonitorType> sysMonitorTypes = sysMonitorTypeMapper.selectList(Wrappers.<SysMonitorType>lambdaQuery());
        return AjaxResult.success(sysMonitorTypes);
    }

    @Override
    public AjaxResult deleteMonitorType(SysMonitorType params) {
        sysMonitorTypeMapper.delete(Wrappers.<SysMonitorType>lambdaQuery().eq(Objects.nonNull(params.getId()), SysMonitorType::getId, params.getId()));
        return AjaxResult.success();
    }

    @Override
    public AjaxResult addMonitorType(SysMonitorType params) {
        sysMonitorTypeMapper.insert(params);
        return AjaxResult.success();
    }
}




